//
//  GoodsInfoTableView.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/1.
//

#import "GoodsInfoTableView.h"
#import "GoodsInfoTextTableViewCell.h"
#import "GoodsInfoImageTableViewCell.h"
#import <SDImageCache.h>
#import <SDWebImageDownloader.h>

@interface GoodsInfoTableView ()

@property (nonatomic, strong) NSMutableArray *imageurlArr;
@end
@implementation GoodsInfoTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.delegate = self;
        self.dataSource = self;
        self.backgroundColor = [UIColor whiteColor];
        self.showsVerticalScrollIndicator = NO;
    }
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        GoodsInfoTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsInfoTextTableViewCell"];
        if (!cell) {
            cell = [[GoodsInfoTextTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GoodsInfoTextTableViewCell"];
        }
        cell.contentStr = self.model.goodsIntroduction;
        return cell;
    }else {
        GoodsInfoImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsInfoImageTableViewCell"];
        if (!cell) {
            cell = [[GoodsInfoImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GoodsInfoImageTableViewCell"];
        }
        [self.imageurlArr removeAllObjects];
        if (self.model.video.length > 0) {
            [self.imageurlArr addObject:self.model.videoPicUrl];
            if (indexPath.row == 1) {
                cell.playImageV.hidden = NO;
            }else {
                cell.playImageV.hidden = YES;
            }
        }
        if (self.model.imgUrl.count > 0) {
            [self.imageurlArr addObjectsFromArray:self.model.imgUrl];
        }
        NSString *url = self.imageurlArr[indexPath.row - 1];
        NSString *str = [[YPDownloadManager sharedDownloadManager] getImagePathWithUrl:url];
        if (str) {
            [cell.imageV sd_setImageWithURL:[NSURL fileURLWithPath:str]];
        }else {
            [[YPDownloadManager sharedDownloadManager] downloadImageWithUrl:url];
            [cell.imageV sd_setImageWithURL:[NSURL URLWithString:url]];
        }
        return cell;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSMutableArray *arr = [NSMutableArray array];
    if (self.model.video.length > 0) {
        [arr addObject:self.model.videoPicUrl];
    }
    if (self.model.imgUrl.count > 0) {
        [arr addObjectsFromArray:self.model.imgUrl];
    }
    return arr.count+1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.subject sendNext:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        CGRect rect = [self.model.goodsIntroduction boundingRectWithSize:CGSizeMake(kWidthForScreen-32, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:kFontRegula(13)} context:nil];
            CGFloat height = rect.size.height + 1 + 20;
        return height;
    }else {
        NSString *imgUrl = self.imageurlArr[indexPath.row-1];
        NSString *path = [[YPDownloadManager sharedDownloadManager] getImagePathWithUrl:imgUrl];
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        if (!image) {
            image = [[SDImageCache sharedImageCache] imageFromCacheForKey:imgUrl];
        }
        if (!image) {
            return kWidthForScreen-32;
        }else {
            CGFloat w = kWidthForScreen - 32;
            CGFloat imageW = 0;
            CGFloat imageH = 0;
            if (image.size.width <= w) {
                imageW = image.size.width;
                imageH = image.size.height;
            }else {
                CGFloat s = image.size.width / w;
                imageW = w;
                imageH = image.size.height / s;
            }
            return imageH+10;
        }
    }
}

- (RACSubject *)subject {
    if (!_subject) {
        _subject = [RACSubject subject];
    }
    return _subject;
}

- (NSMutableArray *)imageurlArr {
    if (!_imageurlArr) {
        _imageurlArr = [NSMutableArray array];
    }
    return _imageurlArr;
}
@end
